<!MILESTONE CALENDAR>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
	<html>
	<head>
        <link href="style/style.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
	<?php
	function showCalendar($formYear, $formMonth, $formDay)
    {
	   // Get key day informations.
	   // We need the first and last day of the month and the actual day
       /*$todayFunction = $today;
         $firstDayFunction = $firstDay;
         $lastDayFunction = $lastDay;*/
    // parse string into current year, current month, current day
        
    $year = $formYear;
    $month = $formMonth;
    $day = $formDay;
    
    //Switch statement to match month name with current month number
    $monthName = "";
    switch($month)
    {
        case 1: 
        $monthName = "January";
        break;
        case 2:
        $monthName = "Febuary";
        break;
        case 3:
        $monthName = "March";
        break;
        case 4:
        $monthName = "April";
        break;
        case 5:
        $monthName = "May";
        break;
        case 6:
        $monthName = "June";
        break;
        case 7:
        $monthName = "July";
        break;
        case 8:
        $monthName = "August";
        break;
        case 9:
        $monthName = "September";
        break;
        case 10:
        $monthName = "October";
        break;
        case 11:
        $monthName = "November";
        break;
        case 12: 
        $monthName = "December";
        break;
        default:
        $monthName = "Error";
        break;
    }    
    
	   //gets first day and last day of month
	   $firstDay = getdate(mktime(0,0,0,$month,1,$year));
	   $lastDay = getdate(mktime(0,0,0,$month+1,0,$year));
	
	   // Create a table with the necessary header informations
	   echo '<table border = 1 width = 100%>';
	   echo ' <tr><th colspan="7">'.$monthName."  ".$year."</th></tr>";
	   echo '<tr class="days">'; 
	   echo ' <td width = 13%>Monday</td><td width = 13%>Tuesday  </td><td width = 13%>Wednesday  </td><td width = 13%>Thursday            </td>';
	   echo ' <td width = 13%>Friday  </td><td width = 13%>Saturday  </td><td width = 13%>Sunday</td></tr>';
	
	   // Display the first calendar row with correct positioning
	   echo '<tr>'; //FORMAT FOR ALL CELLS NOT IN CURRENT MONTH AND FIRST WEEK
	   for($i=1;$i<$firstDay['wday'];$i++)
       {
           //shade cells that are not apart of the current month
	       echo '<td BGCOLOR = F5F5DC >&nbsp</td>'; 
	   }
	   $actday = 0;
	   for($i=$firstDay['wday'];$i<=7;$i++)
       {
	       $actday++;
	       if ($actday == $day) 
           {
	           $class = ' class="actday"';
	       } 
           else 
           {
               //blank for first few days that arnt apart of month
	           $class = '';
	       }
           //display events in cell //DENTON FIRST WEEK OF MONTH
             
	         echo "<td$class BGCOLOR = FFD700><b>$actday</b>
             <i><b><div align = center>Denton</div></b></i>
             <form method=\"POST\" action=\"\">
             <input value =\"+ Add Event\" name = \"nextMonth\" type=\"submit\" style = \"float: right;\">
             </form>
    
             </td>";
           
          
	   }
       echo "</tr>";
       echo "<tr>"; 
        //DISPLAY FOR FIRST WEEK SECOND ROW "AUBREY"
	   for($i=1;$i<$firstDay['wday'];$i++)
       {
           //shade cells that are not apart of the current month
	       echo '<td BGCOLOR = F5F5DC  >&nbsp</td>';
	   }
       $actday = 0;
	   for($i=$firstDay['wday'];$i<=7;$i++)
       {
	       $actday++;
	       if ($actday == $day) 
           {
	           $class = ' class="actday"';
	       } 
           else 
           {
               //blank for first few days that arnt apart of month
	           $class = '';
	       }
           //display events in cell
	         echo "<td$class BGCOLOR =ADFF2F>
             <i><b><div align = center>Aubrey</div></b></i>
             <form method=\"POST\" action=\"\">
             <input value =\"+ Add Event\" name = \"nextMonth\" type=\"submit\" style = \"float: right;\">
             </form>
             </td>";
	   }
	   echo '</tr>';
	
	   //Get how many complete weeks are in the actual month
	   $fullWeeks = floor(($lastDay['mday']-$actday)/7); 
	   for ($i=0;$i<$fullWeeks;$i++)
       {
	       echo '<tr>'; //FORMAT FOR ALL DENTON CELLS new row for every seven days
	       for ($j=0;$j<7;$j++)
           {
	           $actday++; 
	           if ($actday == $day) 
               {
	               $class = ' class="actday"';
	           } 
               else 
               {
	               $class = '';
	           }
	           echo "<td$class BGCOLOR = FFD700><b>$actday</b>
               <i><b><div align = center>Denton</div></b></i>
               <form method=\"POST\" action=\"\">
               <input value =\"+ Add Event\" name = \"nextMonth\" type=\"submit\" style = \"float: right;\">
               </form>
               </td>";
	       }
	       echo '</tr>';
           echo '<tr>'; //FORMAT FOR ALL AUBREY CELLS
	       for ($j=0;$j<7;$j++)
           {
	           echo "<td$class BGCOLOR =ADFF2F>
               <i><b><div align = center>Aubrey</div></b></i>
               <form method=\"POST\" action=\"\">
               <input value =\"+ Add Event\" name = \"nextMonth\" type=\"submit\" style = \"float: right;\">
               </form>
               </td>";
	       }
	       echo '</tr>';
	   }
	   $tempday = $actday;
	   //Now display the rest of the month
	   if ($actday < $lastDay['mday'])
       {
	       echo '<tr>'; 
	       for ($i=0; $i<7;$i++)
           {
	           $actday++;
	           if ($actday == $day) 
               {
	               $class = ' class="actday"';
	           }
               else 
               {
	               $class = '';
	           }
	           //DISPLAY DENTON
	           if ($actday <= $lastDay['mday'])
               {
	                echo "<td$class BGCOLOR = FFD700><b>$actday</b>
                    <i><b><div align = center>Denton</div></b></i>
                    <form method=\"POST\" action=\"\">
                    <input value =\"+ Add Event\" name = \"nextMonth\" type=\"submit\" style = \"float: right;\">
                    </form>
                    </td>";
                   
	           }
	           else 
               {
                   echo '<td BGCOLOR = F5F5DC  >&nbsp</td>'; 
	           }
               
	       } 
	       echo '</tr>';
           
           echo '<tr>'; 
           $actday = $tempday; //set $actday to original value
	       for ($i=0; $i<7;$i++)
           {
	           $actday++;
	           if ($actday == $day) 
               {
	               $class = ' class="actday"';
	           }
               else 
               {
	               $class = '';
	           }
	
	           if ($actday <= $lastDay['mday'])
               {
                     //DISPLAY AUBREY
	                 echo "<td$class BGCOLOR =ADFF2F>
                     <i><b><div align = center>Aubrey</div></b></i>
                     <form method=\"POST\" action=\"\">
                     <input value =\"+ Add Event\" name = \"nextMonth\" type=\"submit\" style = \"float: right;\">
                     </form>
                     </td>";
                  
	           }
	           else 
               {
                   echo '<td BGCOLOR = F5F5DC  >&nbsp</td>'; 
	           }
           }   
     
	   } 
	   echo '</table>';
    }
    ?>
        <h1 align = 'center'>The Milestone Calendar</h1>
        <form method="POST" action="">
            <input value ="Last Month" name = "lastMonth" type="submit" style = "float: left;">
            <!-- pass 0,0,0,1, 0, currentMonth-1, year, day, month, 0 today into                   showCalendar() function-->
        </form>
        
        <form method="POST" action="">
            <input value ="Next Month" name = "nextMonth" type="submit" style = "float: right;">
        </form>
    <?php

    //if last month of next month buttons are pushed then decrement of increment month value. This also takes into account the year
    //place values into showCalendar function
    if(isset($_POST["lastMonth"]))
    {
        session_start(); 
        if($_SESSION['formMonth'] > 1)
        {
            $_SESSION['formMonth'] = $_SESSION['formMonth']-1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
        }
        else if($_SESSION['formMonth'] == 1)
        {
            $_SESSION['formMonth'] = 12;
            $_SESSION['formYear'] = $_SESSION['formYear']-1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
            
        }
    }
    else if(isset($_POST["nextMonth"]))
    {
        session_start(); 
        if($_SESSION['formMonth'] <12)
        {
            $_SESSION['formMonth'] = $_SESSION['formMonth']+1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
        }
        else if($_SESSION['formMonth'] >= 12)
        {
            $_SESSION['formMonth'] = 1;
            $_SESSION['formYear'] = $_SESSION['formYear']+1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
           
        }
    }
    else
    {
        session_start(); 
        $today = date("Y-m-d");
        
        
        $_SESSION['formYear'] = "";
        //YEAR
        for($i =0; $i <4; $i++)
        {
            $yearArray[$i] = $today[$i];  
        }
        //convert array to string
        $_SESSION['formYear'] = implode("", $yearArray);
        $_SESSION['formMonth'] = "";
        $count1= 0;
        //MONTH
        for($i = 5; $i < 7; $i++)
        {
            $monthArray[$count1] = $today[$i];
            $count1++;
        }
        //convert array to string
        $_SESSION['formMonth'] = implode("", $monthArray);
        $_SESSION['formDay'] = "";
        $count1 = 0;
        //DAY
        for($i = 8; $i <10; $i++)
        {
            $dayArray[$count1] = $today[$i];
            $count1++;
        }
        //convert array to string
        $_SESSION['formDay'] = implode("", $dayArray);
	    showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
        
        
    }
	?>
	</body>
	</html>